<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>广告</title>
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/easyui.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/icon.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/neweasyui/themes/gray/panel.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/neweasyui/themes/gray/window.css">
  <script type="text/javascript" src="<%=path %>/script/1.11.1.js"></script>
  <script type="text/javascript" src="<%=path %>/script/jquery.easyui.min.js"></script>
  <script type="text/javascript" src="<%=path %>/script/jquery.easyui.min.js"></script>
  <script type="text/javascript" src="<%=path %>/script/easyui-lang-zh_CN.js"></script>
  <script type="text/javascript" src="<%=path %>/script/dateFormat.js"></script>
  <script type="text/javascript" src="<%=path %>/script/datagrid-bufferview.js"></script>
  <script type="text/javascript" src="<%=path %>/script/common.js"></script>
  <script type="text/javascript" src="<%=path %>/script/ckeditor/ckeditor.js"></script>
  <style type="text/css">
    #fm{
      margin:0;
      padding:10px 30px;
    }
    .ftitle{
      font-size:14px;
      font-weight:bold;
      padding:5px 0;
      margin-bottom:10px;
      border-bottom:1px solid #ccc;
    }
    .fitem{
      margin-bottom:17px;
    }
    .fitem label{
      display:inline-block;
      width:80px;
    }
    .fitem input{
      width:230px;
    }
  </style>
</head>
<body>
<table id="tt" title="class" style="width:auto;height:500px">
</table>
<!-----------------------------------------------------------------按钮------------------------------------------------------------------->
<div id="toolbar" style="padding:5px;height:auto">
  <div>
  	  日期：<input id="day"  name="day" type="text" class="easyui-datebox" style="width:150px" />&nbsp;&nbsp;
      状态：<select id="s_status" name="s_status" class="easyui-combobox"  panelHeight="auto" style="width:auto" data-options="editable:false ">
      			<option value="" selected="selected">全部</option>
      			<option value="0">未活动</option>
      			<option value="1">活动</option>
      		</select>&nbsp;&nbsp;
      广告位：  <input class="easyui-combobox"  id="s_position" 
				name="s_position"
				data-options="
						url:'<%=basePath %>service/combo/ad',
						method:'get',
						valueField:'valueField',
						textField:'textField',
						panelHeight:'auto'
				">&nbsp;&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-search" onclick="searchSentence()">查询</a>
  
    <!-- 搜索条件结束 -->
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-add" onclick="addAd()">添加</a>&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-edit" onclick="editClass()">修改</a>&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-remove" onclick="deleteClass()">删除</a>&nbsp;&nbsp;

  </div>
</div>
<!-------------------------------------------------------------添加/编辑框 开始----------------------------------------------------------------------->
<!-- 添加/编辑框 开始-->
<div id="addad" class="easyui-dialog"
     style="width: 1600px; height: 800px; padding: 10px 20px" closed="true"
     buttons="#dlg-buttons">
  <div class="ftitle">广告信息</div>
  <form id="inAddAd" method="post"  validate enctype="multipart/form-data">
    <input type="hidden" name="id" id="cls_id" />

    <div class="fitem">
      <label>描述:</label>
      <input  class="easyui-textbox" name="description" id="cls_name" value="" />
    </div>
    <div class="fitem">
      <label>图标:</label>
      <input  type="file" name="file" id="imageUrl" value="" required/>
	
    </div>


    <div class="fitem">
      <label>状态:</label>
      <select class="easyui-combobox"   name="status" id="status" required style="width:235px;" panelHeight="auto">
      	<option value="0" >未活动</option>
      	<option value="1"  selected>活动</option>
      </select>	
      
    </div>
    <div class="fitem">
      <label>广告位:</label>
       	<input class="easyui-combobox"  id="positionId" required
		name="positionId"
		data-options="
				url:'<%=basePath %>service/combo/ad',
				method:'get',
				valueField:'valueField',
				textField:'textField',
				panelHeight:'auto'
		">
    </div>
    <div class="fitem">
      <label>内容:</label>
      <textarea  rows="12" cols="80" style="width: 80%" id="content" name="content" ></textarea>
      <script type="text/javascript">CKEDITOR.replace('content');</script>
    </div>
    
  </form>
  <div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6"  iconCls="icon-ok" onclick="saveOrUpdateAd()" style="width: 90px">Save</a> 
    <a href="javascript:void(0)" class="easyui-linkbutton"  iconCls="icon-cancel" onclick="closeDialog()" style="width: 90px">Cancel</a>
  </div>
</div>
<!------------------------------------------------------------------------------------------------------------------------------------>

<!-- 添加/编辑框结束-->




</body>
<script type="text/javascript">
  var class_id,course_id,gate_id;
  $.ajaxSetup({cache:false});
  $(function(){
    $('#tt').datagrid({
      url:'<%=basePath %>service/ad/list?day',
      method:'get',
      singleSelect:true,toolbar:'#toolbar',remoteSort:false,pageSize:50,nowrap:false,
      fitColumns:true,pagination:true,rownumbers:true,
      columns:[[
        {field:'id',title:'ID',sortable:true,width:20,hidden:true},
        {field:'positionId',title:'位置ID',sortable:true,width:60,hidden:true},
        {field:'description',title:'描述',sortable:true,width:80},
        {field:'imageUrl',title:'ICON',sortable:true,width:80},
        {field:'content',title:'内容',sortable:true,width:50,formatter:function(value,row,index){
        	return "请点击修改进行查看";
        	
        }},
        {field:'status',title:'状态',sortable:true,width:50,formatter:function(value,row,index){
        	if(value==0){
        		return "未活动";
        	}else{
        		return "活动";
        	}
        	
        }},
        {field:'createDate',title:'创建时间',sortable:true,width:100,formatter:function(value,row,index){  
            var unixTimestamp = new Date(value);  
            return unixTimestamp.toLocaleString();  
            }  
        }, 
        {field:'updateDate',title:'更新时间',sortable:true,width:100,formatter:function(value,row,index){  
        	var unixTimestamp = new Date(value);
             return  unixTimestamp.toLocaleString();  
            }  
        }, 
        {field:'position',title:'广告位',sortable:true,width:100,hidden:true},
        {field:'name',title:'广告位',sortable:true,width:100}
      ]],
    
    });
  });
//------------------------------------------------------------------------------------------
  //关闭弹出框
  function closeDialog(){
    $('#addad').dialog('close') ;
  }

  //添加广告
  function addAd(){
    $('#addad').dialog('open').dialog('setTitle','添加广告');
    $('#inAddAd').form('clear');
    CKEDITOR.instances.content.setData();
    url = '<%=basePath%>service/ad/add';
  }
  //修改广告
  function editClass(){
    url = '<%=basePath%>service/ad/edit';
    var row = $('#tt').datagrid('getSelected');
    if (row){
      $("#cls_id").val(row.id);
      $("#positionId").val(row.positionId);
      CKEDITOR.instances.content.setData(row.content);
      $('#addad').dialog('open').dialog('setTitle','编辑广告');
      $('#inAddAd').form('load',row);
    }
  }


  function deleteClass(){

    var row = $('#tt').datagrid('getSelected');
    if (row){
      $.messager.confirm('提示','确定要删除这个广告吗？',function(r){
        if (r){
          $.post('<%=basePath%>service/ad/remove',{id:row.id},function(result){
            $('#tt').datagrid('reload');    // reload the data
          },'json');
        }
      });
    }
  }
//---------------------------------------------------------------------------------------------------------------------------
  //添加或修改广告
  function saveOrUpdateAd(){

    $('#inAddAd').form('submit', {
      url : url,
      onSubmit : function() {
        return $(this).form('validate');
      },
      success : function(result) {
    	var Data = eval('(' + result + ')');   	  
        if (result.status) {
          $.messager.show({
            title : 'Error',
            msg : '服务器出错，请稍后再试'
          });
        }else if(Data.status == '-1'){
        	$.messager.show({
                title : '提示',
                msg : '此时有正在活动的广告,请先调整'
              });
        	return;
        }else {
          $('#addad').dialog('close'); // close the dialog
          $('#tt').datagrid('reload'); // reload the data
        }
      }
    });
  }



  function searchSentence(){
	  var day = $("#day").textbox("getValue");
	  var sStatus = $("#s_status").textbox("getValue");
	  var sPosition = $("#s_position").textbox("getValue");
		 var url = '<%=basePath %>service/ad/list?day='+day+'&sStatus='+sStatus+'&sPosition='+sPosition;
		 $("#tt").datagrid({
			 url:'<%=basePath %>service/ad/list?day='+day+'&sStatus='+sStatus+'&sPosition='+sPosition,
			 method:'get',
			 onLoadSuccess:function(data){
				 if(data.total == null){
					 $.messager.alert("提示","没有查询到任何符合条件的数据")
				 }
			 }
		 });
  }

  

</script>
</html>
